<!DOCTYPE html>
<html>
<head>
<title>所有菜单管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<#include "../../../include/resources.html">
<script type="text/javascript">
	var treeGrid;
	var winWidth =  800;
	var winHeight = 380;
	var addTitle = "添加菜单";
	var addUrl =  "/modules/system/menu/menuAddPage.html";
	var editTitle = "编辑菜单";
	var editUrl =  "/modules/system/menu/menuEditPage.html";
	var deleteUrl = "/modules/system/menu/menuDelete.html";	
	var addWinParmeters = "'{0}','"+addTitle+"',"+winWidth+","+winHeight+",'"+addUrl+"'";
	var deleteWinParmeters = "'{0}','"+deleteUrl+"'";
    	$(function() {
		treeGrid = $('#treeGrid').treegrid({
			url : 'menuList.html',
			fit : true,
			fitColumns : true,
			checkbox:true,
			border : false,
            		loadFilter: function(rows){return $.convert(rows.rows); },
			idField : 'id',
			treeField : 'text',
			sortOrder : 'asc',
			pagination:false,
			checkOnSelect : false,
			selectOnCheck : false,
			nowrap : false,
			singleSelect:true,
			columns : [ [
			  {field: 'text',title: '菜单名称', width: 180},
		              { field: 'href', title: '链接地址', width: 375 },  
		              { field: 'sort', title: '排序', width: 80},
		              { field: 'remark', title: '备注', width: 220}, 
			  {	field : 'action',
				title : '操作',
				width :75,
				fixed:true,
				align:'center',
				formatter : function(value, row, index) {
					var str = '';
					str += '<dl>';
					str += '<dt><a href="javascript:void(0);">详情</a><i class="iconfont">&#xe639;</i></dt>' ;
					str += '<dd>';
					str += '<a href="javascript:void(0);" onclick="$.fn.treeGridOptions.addFun('+row.id+',addTitle,winWidth,winHeight,addUrl);" title="'+addTitle+'">添加</a>';
					str += '<a href="javascript:void(0);" onclick="$.fn.treeGridOptions.editFun('+row.id+',editTitle,winWidth,winHeight,editUrl);" title="'+editTitle+'">编辑</a>';
					str += '<a href="javascript:void(0);" onclick="$.fn.treeGridOptions.deleteFun('+row.id+',deleteUrl);">删除</a>';
					str += '</dd>'
					str +=  '</dl>'
					return str;
				}
			} ] ],
			toolbar : '#toolbar'
			
		});
	});
	//平滑数据格式转换
	$.convert = function (rows){
	    if(!rows) return;
		function exists(rows, parentId){
			for(var i=0; i<rows.length; i++){
				if (rows[i].id == parentId) return true;
			}
			return false;
		}		
		var nodes = [];
		// 获取顶级菜单
		for(var i=0; i<rows.length; i++){
			var row = rows[i];
			if (!exists(rows, row.parentId)){
				nodes.push({id:row.id,iconCls:row.iconCls,sort:row.sort,text:row.name,href:row.href,addUser:row.addUser,remark:row.remark});
			}
		}		
		var toDo = [];
		for(var i=0; i<nodes.length; i++){
			toDo.push(nodes[i]);
		}
		while(toDo.length){
			var node = toDo.shift();	// 父级节点
			// 获取子级节点
			for(var i=0; i<rows.length; i++){
				var row = rows[i];
				if (row.parentId == node.id){
					var child = {id:row.id,iconCls:row.iconCls,sort:row.sort,text:row.name,href:row.href,addUser:row.addUser,remark:row.remark};					
					if (node.children){
						node.children.push(child);
					} else {
						node.children = [child];
					}					
					toDo.push(child);
				}
			}
		}
		return nodes;
	}
</script>
</head>
<body style="padding:0 20px;" >
<div class="easyui-layout" data-options="fit : true,border : false">
	<div data-options="region:'north',border:false,collapsible:false" id="searchBox">
            <div class="sRight">
				<a onclick="$.fn.treeGridOptions.addFun(0,addTitle,winWidth,winHeight,addUrl);" href="javascript:void(0);" class="addBtn"><i class="iconfont">&#xe629;</i>添加菜单</a>				
            </div>
		</div>
	<div data-options="region:'center'" style="margin-top:30px;padding-bottom:30px;">
		<table id="treeGrid"></table>
	</div>
</div>
</body>
</html>
